package com.example.project.service.impl;

import com.example.project.dao.IRoleDao;
import com.example.project.entity.Role;
import com.example.project.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *作者：金照富
 * 2023年10月31日
 */
@Service
public class IRoleServiceImpl implements IRoleService {

    @Autowired
    private IRoleDao roleMapper;
    @Override
    public int insertRole(Role role) {
        int roleValue;
        Role role1=new Role();
        role1.setRoleName(role.getRoleName());
        List<Role> allRole=roleMapper.getAllRole(role1);
        if(allRole.size()>=1){
            roleValue = -1;
        }else {
            roleValue= roleMapper.insertRole(role);
        }
        return roleValue;
    }

    @Override
    public int deleteRole(Role role) {
        int roleValue;
        Role role1=new Role();
        role1.setRoleName(role.getRoleName());
        List<Role> allRole=roleMapper.getAllRole(role1);
        if(allRole.size()==0){
            roleValue = -1;
        }else {
            roleValue= roleMapper.deleteRole(role);
        }
        return roleValue;
    }

    @Override
    public int updateRole(Role role) {
        int roleValue;
        Role role1=new Role();
        role1.setRoleName(role.getRoleName());
        List<Role> allRole=roleMapper.getAllRole(role1);
        if(allRole.size()>=1){
            roleValue = -1;
        }else {
            roleValue= roleMapper.updateRole(role);
        }
        return roleValue;
    }

    @Override
    public List<Role> getAllRole(Role role) {
        return roleMapper.getAllRole(role);
    }

    @Override
    public Role getRole(Role role) {
        return roleMapper.getRole(role);
    }
}
